﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.DB;
using YT.Controls;
using YT.Commons;
using YT.Modules;

namespace YT.XH.Dizziness
{
    public partial class frmDZExamBeforeTreatTitle : Form
    {

        private string mstrSickRecID = string.Empty;
        private FormStatus mfrmStatus;
        private string mstrTableName = "ExamBeforTreatResult";
        private string mstrSeqNo = string.Empty;
        ArrayList marrlist = new ArrayList();
        private DBAccess mdbcon = new DBAccess(false, false);

        public frmDZExamBeforeTreatTitle()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }

       public frmDZExamBeforeTreatTitle(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID; 
        }

       public frmDZExamBeforeTreatTitle(string strSickRecID, FormStatus frmStauts, string strExamSeqNo)
        {
            mfrmStatus = frmStauts;
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mstrSeqNo = strExamSeqNo;
        }

       private void btnSave_Click(object sender, EventArgs e)
       {
           string strSeqNo = string.Empty;
           DBAccess dbcon = new DBAccess(false, false);
           string strSQL = string.Empty;

           strSQL = "if not exists ( select * from ExamBeforeTreatHdr where SeqNo='" + CvtFunc.ToDouble ( mstrSeqNo) + "' and  SickRecID='" + mstrSickRecID + "' and Type ='" + TitleType.Dizziness + "'  ) ";
           strSQL += " Insert into ExamBeforeTreatHdr(  SickRecID, SeqNo, ExamDate,Type ) " +
                    " values (  '" + mstrSickRecID + "','" + CvtFunc.ToDouble(mstrSeqNo) + "','" + BaseFunctions.FormatDate(dtpCheckDate.Value.ToString(), DateFormatOption.DateFmtYMD) + "' " +
                    " ,'" + TitleType.Dizziness + "' ) ";
           dbcon.ExecuteSql(strSQL);
           if (mstrSeqNo == "" || mstrSeqNo == "0") mstrSeqNo = "1";
           UpdateData(mstrSeqNo);
           mfrmStatus = FormStatus.Update;
           this.Close();  
       }

       private void FindPanel(Control cons)
       {
           foreach (Control con in cons.Controls)
           {
               if (con.GetType().Name == "Panel" && con.Tag != "" && con.Tag != null)
               {
                   marrlist.Add(con);
               }
               if (con.Controls.Count > 0)
               {
                   FindPanel(con);
               }
           }
       }

       private void FillData()
       {
           if (mstrSeqNo=="" || mstrSeqNo =="0") 
            mstrSeqNo ="1";
           string strTableName = "ExamBeforTreatResult";
           DBAccess dbcon = new DBAccess(false, false);
           foreach (Control cons in this.Controls)
           {
               FindPanel(cons);
           }
           foreach (Control conDtl in marrlist)
           {
               if (conDtl.GetType().Name == "Panel")
               {
                   string[] strInfoSeqNo = new string[2];
                   strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                   string strHdrSeqNo = strInfoSeqNo[0];
                   string strDtlSeqNo = strInfoSeqNo[1];
                   string strSickRecID = mstrSickRecID;
                   string strColTitle = string.Empty;
                   string strResult = string.Empty;
                   string strTitleType = TitleType.Dizziness.ToString();
                   foreach (Control conText in conDtl.Controls)
                   {
                       if (conText.Tag != "" && conText.Tag != null)
                       {
                           strColTitle = conText.Tag.ToString();
                           string strSQL = "select top 1 Result from " + strTableName + " where rowno='" + strColTitle + "' and  ExamSeqNo='" + CvtFunc.ToDouble(mstrSeqNo) + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and Type='" + TitleType.Dizziness + "' and SickRecID='" + mstrSickRecID + "'";
                           DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                           if (drCheck == null) continue;
                           strResult = CvtFunc.ToString(drCheck["Result"]);
                           if (conText.GetType().Name == "TextBox" )
                           {
                               conText.Text = strResult;
                           }
                           else if (conText.GetType().Name == "DateTimePicker")
                           {
                               ((DateTimePicker)conText).Value = CvtFunc.ToDateTime(strResult);  
                           }
                           else if (conText.GetType().Name == "RadioButton")
                           {
                               if (strResult == "false")
                                   ((RadioButton)conText).Checked = false;
                               else if (strResult == "true")
                                   ((RadioButton)conText).Checked = true;
                           }
                           else if (conText.GetType().Name == "CheckBox")
                           {
                               if (strResult == "false")
                                   ((CheckBox)conText).Checked = false;
                               else if (strResult == "true")
                                   ((CheckBox)conText).Checked = true;
                           }
                           else if (conText.GetType().Name == "Label")
                           {
                               continue ;
                           }
                       }
                   }

               }
           }
           dbcon.CloseCon();
       }

       private void UpdateData(string strExamSeqNo)
       {
           string strTableName = "ExamBeforTreatResult";
           DBAccess dbcon = new DBAccess(false, false);
           foreach (Control conDtl in marrlist)
           {
               if (conDtl.GetType().Name == "Panel")
               {
                   string[] strInfoSeqNo = new string[2];
                   strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                   string strHdrSeqNo = strInfoSeqNo[0];
                   string strDtlSeqNo = strInfoSeqNo[1];
                   string strSickRecID = mstrSickRecID;
                   string strColTitle = string.Empty;
                   string strResult = string.Empty;
                   DataRow drRow = null;
                   string strFields = "SickRecID,HdrSeqno,DtlSeqNo,RowNo,ColTitle,Result,Type,ExamSeqNo";
                   foreach (Control conText in conDtl.Controls)
                   {
                       if (conText.Tag != "" && conText.Tag != null)
                       {
                           if (conText.GetType().Name == "TextBox" )
                           {
                               strResult = conText.Text;
                           }
                           else if (conText.GetType().Name == "DateTimePicker")
                           {
                               strResult = BaseFunctions.FormatDate(((DateTimePicker)conText).Value.ToString(), DateFormatOption.DateFmtYMD); 
                           }
                           else if (conText.GetType().Name == "RadioButton")
                           {
                               strResult = (((RadioButton)conText).Checked ? "true" : "false");
                           }
                           else if (conText.GetType().Name == "CheckBox")
                           {
                               strResult = (((CheckBox)conText).Checked ? "true" : "false");
                           }
                           else if (conText.GetType().Name == "Label")
                           {
                               strResult = "";
                               continue;
                           }
                           strColTitle = conText.Tag.ToString();

                           string strSQL = "select top 1 * from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and ExamSeqNo='" + mstrSeqNo + "' and Type='" + TitleType.Dizziness + "' and SickRecID='" + mstrSickRecID + "'";
                           bool blnIsNew = false;
                           DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                           if (drCheck == null)
                           {
                               blnIsNew = true;
                               drRow = dbcon.GetDataTable("select top 0 * from " + strTableName + " ").NewRow();
                           }
                           else
                           {
                               blnIsNew = false;
                               drRow = dbcon.GetDataFirstRow("select top 1 * from " + strTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                           }

                           drRow["SickRecID"] = strSickRecID;
                           drRow["HdrSeqno"] = strHdrSeqNo;
                           drRow["DtlSeqNo"] = strDtlSeqNo;
                           drRow["RowNo"] = strColTitle;
                           drRow["Result"] = strResult;
                           drRow["Type"] = TitleType.Dizziness ;
                           drRow["ExamSeqNo"] = strExamSeqNo;
                           if (blnIsNew)
                               DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.AddNew);
                           else
                               DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.Update);
                       }
                   }
               }
           }
           dbcon.CloseCon();
       }

       private void btnExit_Click(object sender, EventArgs e)
       {
           this.Close();
       }

       private void frmDZExamBeforeTreatTitle_Load(object sender, EventArgs e)
       {
           this.Text = "治疗前检查";
           FillData();
           timer1.Start();
       }

       private void frmDZExamBeforeTreatTitle_FormClosed(object sender, FormClosedEventArgs e)
       {
           SysVar.marrFormList.Remove(this.Name);
           frmGifImage frmGIF = new frmGifImage();
           frmGIF.TopMost = true;
           frmGIF.Show();
       }

       private void timer1_Tick(object sender, EventArgs e)
       {
           if (this.Opacity >= 1)
               timer1.Stop();
           this.Opacity += 0.04;
       }
    }
}
